#!/bin/sh
#Barry Kauler april 2008, for Puppy Linux, www.puppylinux.com. GPL (ref: /usr/share/doc/legal)
# hacked for nls support and slight improvements, Nathan Fisher
#v426 BK: bugfix 's off' was not enough to disable screensaver, added '-dpms'.
#130209 BK: need LANG=C for 'dc'. do it for 'xset' also.
#130209 BK: convert "technosaurus method" nls to gettext i18n translation. now v0.4.

export TEXTDOMAIN=pupx
export OUTPUT_CHARSET=UTF-8

PAGE=0 #mouse
case $1 in
	keyboard) PAGE=1 ;;
	screensaver) PAGE=2 ;;
esac

XSETQ="`LANG=C xset q`" #130209

MOUSEACCEL10=`echo "$XSETQ" | grep '  acceleration:' | grep ' threshold:' | tr -s ' ' | cut -f 3 -d ' ' | cut -f 1 -d '/'`
MOUSEACCELDIV=`echo "$XSETQ" | grep '  acceleration:' | grep ' threshold:' | tr -s ' ' | cut -f 3 -d ' ' | cut -f 2 -d '/'`
MOUSEACCEL=`LANG=C dc ${MOUSEACCEL10} ${MOUSEACCELDIV} div p` #130209
MOUSETHRESHOLD=`echo "$XSETQ" | grep '  acceleration:' | grep ' threshold:' | tr -s ' ' | cut -f 5 -d ' '`
KEYBOARDVOL="`echo "$XSETQ" | grep 'key click percent: ' | tr -s ' ' | cut -f 8 -d ' '`"
KEYBOARDAUTO="`echo "$XSETQ" | grep ' auto repeat: ' | tr -s ' ' | cut -f 4 -d ' '`"
if [ "$KEYBOARDAUTO" = "on" ];then
 KEYBOARDAUTO="yes"
 KBVISIBLE="enabled"
fi
if [ "$KEYBOARDAUTO" = "off" ];then
 KEYBOARDAUTO="no"
 KBVISIBLE="disabled"
fi
KEYBOARDDELAY="`echo "$XSETQ" | grep ' auto repeat delay: ' | tr -s ' ' | cut -f 5 -d ' '`"
KEYBOARDRATE="`echo "$XSETQ" | grep ' auto repeat delay: ' | grep 'repeat rate:' | tr -s ' ' | cut -f 8 -d ' '`"
SCREENSAVERDELAY="`echo "$XSETQ" | grep ' timeout: ' | grep ' cycle: ' | tr -s ' ' | cut -f 3 -d ' '`"
SCREENSAVERFLAG='yes'
SCREENVISIBLE="enabled"
if [ "$SCREENSAVERDELAY" = "0" ];then
 SCREENSAVERFLAG='no'
 SCREENVISIBLE="disabled"
fi
SCREENSAVERCYCLE="`echo "$XSETQ" | grep ' timeout: ' | grep ' cycle: ' | tr -s ' ' | cut -f 5 -d ' '`"
SCREENSAVERBLANKING="`echo "$XSETQ" | grep ' prefer blanking: ' | tr -s ' ' | cut -f 4 -d ' '`"
SCREENSAVEREXPOSURES="`echo "$XSETQ" | grep ' allow exposures: ' | tr -s ' ' | cut -f 7 -d ' '`"

#precautions...
[ "$MOUSEACCEL" = "" ] && MOUSEACCEL='2.0'
[ "$MOUSETHRESHOLD" = "" ] && MOUSETHRESHOLD='4'
[ "$KEYBOARDVOL" = "" ] && KEYBOARDVOL='0'
[ "$KEYBOARDAUTO" = "" ] && KEYBOARDAUTO='yes'
[ "$KEYBOARDDELAY" = "" ] && KEYBOARDDELAY='500'
[ "$KEYBOARDRATE" = "" ] && KEYBOARDRATE='6'
[ "$SCREENSAVERFLAG" = "" ] && SCREENSAVERFLAG='yes'
[ "$SCREENSAVERDELAY" = "" ] && SCREENSAVERDELAY='600'
[ "$SCREENSAVERCYCLE" = "" ] && SCREENSAVERCYCLE='600'
[ "$SCREENSAVERBLANKING" = "" ] && SCREENSAVERBLANKING='yes'
[ "$SCREENSAVEREXPOSURES" = "" ] && SCREENSAVEREXPOSURES='yes'

function pupx_mouseaccel() {
	MOUSEACCX10=`LANG=C dc ${MOUSEACCEL} 10 mul p` #130209
	LANG=C xset m ${MOUSEACCX10}/10 ${MOUSETHRESHOLD} #130209
	echo -n "m ${MOUSEACCX10}/10 ${MOUSETHRESHOLD}" >> /tmp/pupx_finalparams
	[ ! "$1" ] && Xdialog --title "Mouse settings" --msgbox "Mouse acceleration set to ${1}\nMouse threshold set to ${2}\n(for this session only)" 0 0
}

function pupx_keyboard() {
	KEYBOARDDELAY=`echo -n $KEYBOARDDELAY | cut -f 1 -d '.'`
	KEYBOARDRATE=`echo -n $KEYBOARDRATE | cut -f 1 -d '.'`
	[ $KEYBOARDVOL -eq 0 ] && KEYBOARDVOL=off
	if [ "$KEYBOARDAUTO" = "false" ];then
		KEYBOARDPARAMS="off"
	else
		KEYBOARDPARAMS="rate $KEYBOARDDELAY $KEYBOARDRATE"
	fi
	LANG=C xset c $KEYBOARDVOL r $KEYBOARDPARAMS #130209
	echo -n " c $KEYBOARDVOL r $KEYBOARDPARAMS"  >> /tmp/pupx_finalparams
	[ ! "$1" ] && Xdialog --title "Keyboard settings" --msgbox "Keyclick volume set to ${KEYBOARDVOL}\nAuto-repeat set to ${KEYBOARDPARAMS}\n(for this session only)" 0 0
}

#pupx_screen \$SCREENSAVERFLAG \$SCREENSAVERDELAY \$SCREENSAVERCYCLE \$SCREENSAVERBLANKING \$SCREENSAVEREXPOSURES
function pupx_screen() { #add '-dpms' when turn off screensaver...
	SCREENSAVERDELAY=`echo -n $SCREENSAVERDELAY | cut -f 1 -d '.'`
	SCREENSAVERCYCLE=`echo -n $SCREENSAVERCYCLE | cut -f 1 -d '.'`
	if [ "$SCREENSAVERFLAG" = "false" ];then
	 SCREENSAVERPARAMS="s off -dpms"
	else
	 [ "$SCREENSAVERBLANKING" = "false" ] && SCREENSAVERBLANKING=noblank
	 [ "$SCREENSAVERBLANKING" = "true" ] && SCREENSAVERBLANKING=blank
	 [ "$SCREENSAVEREXPOSURES" = "false" ] && SCREENSAVEREXPOSURES=noexpose
	 [ "$SCREENSAVEREXPOSURES" = "true" ] && SCREENSAVEREXPOSURES=expose
	 SCREENSAVERPARAMS="s $SCREENSAVERDELAY $SCREENSAVERCYCLE s $SCREENSAVERBLANKING s $SCREENSAVEREXPOSURES"
	fi
	LANG=C xset $SCREENSAVERPARAMS
	echo -n " $SCREENSAVERPARAMS"  >> /tmp/pupx_finalparams
	[ ! "$1" ] && Xdialog --title "Screensaver settings" --msgbox "Screensaver parameters set to:\n${SCREENSAVERDELAY} ${SCREENSAVERCYCLE} ${SCREENSAVERBLANKING} ${SCREENSAVEREXPOSURES}\n(for this session only)" 0 0
}

export -f pupx_mouseaccel
export -f pupx_keyboard
export -f pupx_screen

combo_items() {
	for i in $@ ; do echo "<item>${i}</item>" ; done
}

export MAIN_DIALOG='<window title="'$(gettext 'pupX: set properties of X')'" icon-name="video-display">
<vbox>
  <notebook page="'${PAGE}'" labels="'$(gettext 'Mouse')'|'$(gettext 'Keyboard')'|'$(gettext 'Screensaver')'">
  
    <vbox>
     <hbox>
      <text>
       <label>'$(gettext 'Acceleration:')'</label>
      </text>
      <combobox>
       <variable>MOUSEACCEL</variable>
       '$(combo_items ${MOUSEACCEL} 7.0 6.5 6.0 5.5 5.0 4.5 4.0 3.5 3.0 2.5 2.0 1.8 1.6 1.4 1.2 1.0)'
      </combobox>
     </hbox>
     <text><label>'$(gettext "The mouse will accelerate with this factor when moved more than 'threshold' pixels in a short time")'</label></text>
     <hbox>
      <text>
       <label>'$(gettext 'Threshold:')'</label>
      </text>
      <combobox>
       <variable>MOUSETHRESHOLD</variable>
       '$(combo_items ${MOUSETHRESHOLD} 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1)'
      </combobox>
     </hbox>
     <text><label>'$(gettext 'Acceleration will happen when the mouse is moved this many pixels in a short time')'</label></text>
    </vbox>

    <vbox>
     <hbox>
      <text><label>'$(gettext 'Volume:')'</label></text>
      <combobox>
       <variable>KEYBOARDVOL</variable>
       '$(combo_items ${KEYBOARDVOL} 100 90 80 70 60 50 40 30 20 10 0)'
      </combobox>
     </hbox>
     <text><label>'$(gettext 'Keyclick volume in percentage (0-100)')'</label></text>
     <frame '$(gettext 'Auto-repeat')'>
      <checkbox>
       <label>'$(gettext 'Enable auto-repeat')'</label>
       <variable>KEYBOARDAUTO</variable>
       <default>'${KEYBOARDAUTO}'</default>
       <action>if true enable: KEYBOARDDELAY</action>
       <action>if true enable: KEYBOARDRATE</action>
       <action>if false disable: KEYBOARDDELAY</action>
       <action>if false disable: KEYBOARDRATE</action>
      </checkbox>
      <hbox>
       <text><label>"       "</label></text>
       <text><label>'$(gettext 'Delay:')'</label></text>
       <entry max_length="4">
        <visible>'${KBVISIBLE}'</visible>
        <default>'${KEYBOARDDELAY}'</default>
        <variable>KEYBOARDDELAY</variable>
       </entry>
       <text><label>'$(gettext '(milliseconds)       ')'</label></text>
      </hbox>
      <text><label>'$(gettext 'This is the delay before keys start repeating')'</label></text>
      <hbox>
       <text><label>"        "</label></text>
       <text><label>'$(gettext 'Rate:')'</label></text>
       <entry max_length="2">
        <visible>'${KBVISIBLE}'</visible>
        <default>'${KEYBOARDRATE}'</default>
        <variable>KEYBOARDRATE</variable>
       </entry>
       <text><label>'$(gettext '(repeats per second)       ')'</label></text>
      </hbox>
      <text><label>'$(gettext 'This is the rate at which keys repeat')'</label></text>
     </frame>
    </vbox>

    <vbox>
      <checkbox>
       <label>'$(gettext 'Enable screen saver')'</label>
       <variable>SCREENSAVERFLAG</variable>
       <default>'${SCREENSAVERFLAG}'</default>
       <action>if true enable: SCREENSAVERDELAY</action>
       <action>if true enable: SCREENSAVERCYCLE</action>
       <action>if false disable: SCREENSAVERDELAY</action>
       <action>if false disable: SCREENSAVERCYCLE</action>
      </checkbox>
      <hbox>
       <text><label>"       "</label></text>
       <text><label>'$(gettext 'Delay:')'</label></text>
       <entry max_length="4">
        <visible>'${SCREENVISIBLE}'</visible>
        <default>'${SCREENSAVERDELAY}'</default>
        <variable>SCREENSAVERDELAY</variable>
       </entry>
       <text><label>'$(gettext '(seconds)       ')'</label></text>
      </hbox>
      <text><label>'$(gettext 'This is the delay before screensaver is activated')'</label></text>
      <hbox>
       <text><label>"        "</label></text>
       <text><label>'$(gettext 'Cycle:')'</label></text>
       <entry max_length="4">
        <visible>'${SCREENVISIBLE}'</visible>
        <default>'${SCREENSAVERCYCLE}'</default>
        <variable>SCREENSAVERCYCLE</variable>
       </entry>
       <text><label>'$(gettext '(seconds)       ')'</label></text>
      </hbox>
      <text><label>'$(gettext 'This is the interval for change of background pattern')'</label></text>
      <hbox>
       <text><label>"   "</label></text>
       <vbox>
        <checkbox>
         <label>'$(gettext 'Blank the screen rather than display a pattern')'</label>
         <variable>SCREENSAVERBLANKING</variable>
         <default>'${SCREENSAVERBLANKING}'</default>
        </checkbox>
        <checkbox>
         <label>'$(gettext 'Allow exposures')'</label>
         <variable>SCREENSAVEREXPOSURES</variable>
         <default>'${SCREENSAVEREXPOSURES}'</default>
        </checkbox>
       </vbox>
      </hbox>
    </vbox>
    
  </notebook>
  
  <hbox>
   <button cancel>
    <action type="exit">no save</action>
   </button>
   <button>
    <input file stock="gtk-apply"></input>
    <label>'$(gettext 'Apply')'</label>
    <action>pupx_mouseaccel &</action>
    <action>pupx_keyboard &</action>
    <action>pupx_screen &</action>
   </button>
   <button ok>
    <action type="exit">save settings</action>
   </button>
  </hbox>
</vbox>
</window>
'

RETPARAMS="`gtkdialog3 --program=MAIN_DIALOG`"
eval "$RETPARAMS"
echo "$RETPARAMS"
[ "$EXIT" != "save settings" ] && exit

echo -n "" > /tmp/pupx_finalparams
pupx_mouseaccel quiet
pupx_keyboard quiet
pupx_screen quiet
sync
echo -n "xset " > $HOME/.xset.sh
cat /tmp/pupx_finalparams ; echo
cat /tmp/pupx_finalparams >> $HOME/.xset.sh
chmod +x $HOME/.xset.sh
#...this is evaluated at bootup by $HOME/.xinitrc
rm -f /tmp/pupx_* # Cleanup

###END###

